package domotica.scontrol.rcp.gui.view;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

public class ConsoleView implements IOutputView {

	private Text text;
	
//	private ListViewer viewer;
//	private Display display;
//	private ConsoleView consoleView;
	
	public ConsoleView(Composite parent) {
		text = new Text(parent, SWT.READ_ONLY | SWT.MULTI);  
		OutputStream out = new OutputStream() {   
			@Override   
			public void write(int b) throws IOException {    
				if (text.isDisposed()) 
					return;
				text.append(String.valueOf((char) b));   
			}  
		}; 
		
		final PrintStream oldOut = System.out;  
		System.setOut(new PrintStream(out));
		
		text.addDisposeListener(new DisposeListener() {   
			public void widgetDisposed(DisposeEvent e) {    
				System.setOut(oldOut);   
			}  
		});
		
//		viewer = new ListViewer(parent);
//		viewer.setContentProvider(new ContentProvider());
//		display = parent.getDisplay();
//		consoleView = this;
//		
//		OutputStream out = new OutputStream() {   
//			@Override   
//			public void write(final int b) throws IOException {    
//				if (display.isDisposed()) 
//					return;
//				display.asyncExec(new Runnable() {
//					public void run(){
//						viewer.setInput(String.valueOf((char) b));
//					}
//				});
//			}  
//		}; 
////		
////		final PrintStream oldOut = System.out;  
//		System.setOut(new PrintStream(out));
	}
	
	@Override
	public void addOutput(String msg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		text.setFocus();
//		display.getFocusControl().setFocus();
	}
	
	@Override
	public void dispose() {
	}

}
